package com.envocabulary.widget_list;

import java.util.Locale;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.envocabulary.Ultis.Reference;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;

public class DetailActivity extends Activity implements
		TextToSpeech.OnInitListener {
	private TextView tvWord;
	private TextView tvPhone;
	private TextView tvMean;
	private ImageView mImgSound;
	private TextToSpeech tts;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.detail);
		tts = new TextToSpeech(this, this);
		// Look up the AdView as a resource and load a request.
		AdView adView = (AdView) this.findViewById(R.id.adView1);
		AdRequest adRequest = new AdRequest.Builder()
				.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
				// Emulator
				.addTestDevice("D2CFFD5B967879F")
				.addTestDevice("2EFFE071A2F36DEF").build();
		adView.loadAd(adRequest);

		tvWord = (TextView) findViewById(R.id.detail_text_word);
		tvPhone = (TextView) findViewById(R.id.detail_text_phonetic);
		tvMean = (TextView) findViewById(R.id.detail_text_meaning);

		mImgSound = (ImageView) findViewById(R.id.detail_speech);
		Intent intent = getIntent();
		String text = intent
				.getStringExtra(ListWidgetProvider.EXTRA_LIST_ITEM_TEXT);

		String meaning = intent
				.getStringExtra(ListWidgetProvider.EXTRA_LIST_ITEM_MEANING);
		String phonetic = intent
				.getStringExtra(ListWidgetProvider.EXTRA_LIST_ITEM_PHONETIC);

		tvWord.setText(text);
		tvPhone.setText(phonetic);
		tvMean.setText(meaning);
		if (Reference.TYPE_WORK == 3) {
			String past = intent
					.getStringExtra(ListWidgetProvider.EXTRA_LIST_ITEM_PAST_SIMPLE);
			String part = intent
					.getStringExtra(ListWidgetProvider.EXTRA_LIST_ITEM_ID);
			TextView mTextPast = (TextView) findViewById(R.id.detail_text_past);
			TextView mTextPart = (TextView) findViewById(R.id.detail_text_part);
			mTextPast.setVisibility(View.VISIBLE);
			mTextPart.setVisibility(View.VISIBLE);
			mTextPast.setText(past);
			mTextPart.setText(part);
			tvMean.setText("Ví dụ: " + meaning);

		}
		mImgSound.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				speakOut();
			}

		});

	}

	private void speakOut() {

		String text = tvWord.getText().toString();

		tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
	}

	@Override
	public void onInit(int status) {
		// TODO Auto-generated method stub

		if (status == TextToSpeech.SUCCESS) {

			int result = tts.setLanguage(Locale.US);

			// tts.setPitch(5); // set pitch level

			// tts.setSpeechRate(2); // set speech speed rate

			if (result == TextToSpeech.LANG_MISSING_DATA
					|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
				Log.e("TTS", "Language is not supported");
			} else {
				mImgSound.setEnabled(true);
				speakOut();
			}

		} else {
			Log.e("TTS", "Initilization Failed");
		}

	}

	@Override
	public void onDestroy() {
		// Don't forget to shutdown!
		if (tts != null) {
			tts.stop();
			tts.shutdown();
		}
		super.onDestroy();
	}
}
